package ltd.sumfsy.grain.service.impl;

import ltd.sumfsy.grain.bean.LimitConfig;
import ltd.sumfsy.grain.bean.example.LimitConfigExample;
import ltd.sumfsy.grain.mapper.LimitConfigMapper;
import ltd.sumfsy.grain.service.LimitConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 阈值配置表相关服务实现类
 * @author 范颂扬
 * @create 2021-05-13 18:00
 */
@Service
public class LimitConfigServiceImpl implements LimitConfigService {

    private final LimitConfigMapper limitConfigMapper;

    @Autowired
    public LimitConfigServiceImpl(LimitConfigMapper limitConfigMapper) {
        this.limitConfigMapper = limitConfigMapper;
    }

    @Override
    public boolean set(LimitConfig limitConfig) {
        int res = limitConfigMapper.insertSelective(limitConfig);
        return res > 0;
    }

    @Override
    public boolean deleteByEquip(Integer equipId) {
        LimitConfigExample example = new LimitConfigExample();
        example.createCriteria().andDelFlagEqualTo(0).andEquipIdEqualTo(equipId);
        int res = limitConfigMapper.updateByExampleSelective(
                new LimitConfig().setDelFlag(1), example);
        return res > 0;
    }

    @Override
    public boolean updateByEquip(LimitConfig limitConfig) {
        LimitConfigExample example = new LimitConfigExample();
        example.createCriteria().andDelFlagEqualTo(0)
                .andEquipIdEqualTo(limitConfig.getEquipId());
        int res = limitConfigMapper.updateByExampleSelective(limitConfig, example);
        return res > 0;
    }

    @Override
    public List<LimitConfig> queryAll() {
        LimitConfigExample example = new LimitConfigExample();
        example.createCriteria().andEquipIdEqualTo(0);
        return limitConfigMapper.selectByExample(example);
    }

    @Override
    public LimitConfig queryByEquipId(Integer equipId) {
        LimitConfigExample example = new LimitConfigExample();
        example.createCriteria().andEquipIdEqualTo(equipId).andDelFlagEqualTo(0);
        List<LimitConfig> limitConfigs = limitConfigMapper.selectByExample(example);
        if (limitConfigs == null || limitConfigs.size() == 0) {
            return null;
        } else {
            return limitConfigs.get(0);
        }
    }
}
